// pages/pay-select/index.js
let wxpay = require('../../utils/pay.js')
let formatTime = require('../../utils/util.js')
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    balanceChecked: true,
    wxChecked: false,
    money: '0.00',
    restMoney: '0.00',
    orderID: '',
    // createTime: '',
    restTime: { day: '0', hour: '00', minute: '00', second: '00' },
    interval: '',
    fromID: 0,
    toOrderList: true 

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    console.log(e.money)
    let that = this
    this.setData({
      money: e.money,
      orderID: e.orderid,
      createTime: e.createtime,
      fromID: e.fromID 
    })

    this.data.interval = setInterval(function () {
      let date = new Date(formatTime.formatTime(new Date(e.createtime)))
      console.log(e.createtime)
      //console.log(date)
      date.setTime(date.getTime() + 24 * 60 * 60 * 1000)
      //console.log(date)
      let restTimeTemp = formatTime.restTime(new Date(), date)

      that.setData({
        restTime: restTimeTemp
      })

    }, 1000)

    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getMoney()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let that = this
    clearInterval(that.data.interval)
    if (this.data.fromID == 0 && this.data.toOrderList)
      wx.navigateTo({
        url: "/pages/order-list/index?type=1"
      })
  },

  toBalance: function () {
    wx.navigateTo({
      url: "/pages/balance/index"
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  checkBalance: function () {
    this.setData({
      balanceChecked: true,
      wxChecked: false
    })
  },

  checkWX: function () {
    this.setData({
      balanceChecked: false,
      wxChecked: true
    })
  },

/**
* 获取余额
*/
  getMoney: function () {
    let token = wx.getStorageSync('token')
    let that = this
    console.log('getmoney')
    wx.request({
      url: app.apiUrl('/user/money'),
      method: 'GET',
      header: {
        "Authorization": 'Bearer ' + token
      },
      data: {

      },
      success: function (res) {
        console.log(res.data)
        if (res.data.code >= app.globalData.successCode) {

          wx.showToast({
            title: res.data.error_message ? res.data.error_message : app.globalData.unknownError,
            icon: 'none',
            duration: 2000
          })
        } else {
          that.setData({
            restMoney: res.data.data.money.toFixed(2)
          })
        }

      },
      fail: function (res) {
        console.log(res)
      },
      complete: function (res) {

      }
    })
  },

  /**
   * 确认支付
   */
  confirmPay: function () {
    if (this.data.balanceChecked)
      this.balancePay()
    else 
      wxpay.wxpay(app, this, this.data.orderID, "/pages/pay-result/index?type=1&money=" + this.data.money, 1, 0, 0)
  },

  balancePay: function () {
    let token = wx.getStorageSync('token')
    let that = this
    if (wx.showLoading) {
      wx.showLoading({
        title: '加载中...',
        mask: true
      })
    } else {
      wx.showToast({
        title: '加载中...',
        mask: true,
        duration: app.globalData.toastTimeout,
        icon: 'none',
      })
    }

    console.log('balancepay')
    wx.request({
      url: app.apiUrl('/order/balancepay'),
      method: 'GET',
      header: {
        "Authorization": 'Bearer ' + token
      },
      data: {
        id: that.data.orderID
      },
      success: function (res) {
        
        if (res.data.code > app.globalData.successCode) {
          wx.showToast({
            title: res.data.error_message ? res.data.error_message : app.globalData.unknownError,
            icon: 'none',
            duration: 2000
          })
        } else if (res.data.code == app.globalData.successCode) {
          app.login(that, 1)
        } else {
          that.data.toOrderList = false
          wx.redirectTo({
            url: "/pages/pay-result/index?type=0&money=" + that.data.money
          })
        }

      },
      fail: function (res) {
        wx.showModal({
          title: '提示',
          content: '网络请求异常',
          showCancel: false
        })
      },
      complete: function (res) {
        if (wx.showLoading)
          wx.hideLoading()
        else
          wx.hideToast()
      }
    })
  }

})